//
// Created by fangp on 3/6/18.
//


#include "runner_ds.h"

static void test_alloc_stack_node() {
    StackNode *node = alloc_stack_node();
    assert_non_null(node);
}

static void test_init_stack() {
    Stack *stack = init_stack();
    assert_non_null(stack);
    assert_null(stack->element);
    assert_null(stack->next);
}

static void test_is_stack_empty() {
    Stack *stack = init_stack();
    int empty = is_stack_empty(stack);
    assert_true(empty);
    push(&stack, "A");
    empty = is_stack_empty(stack);
    assert_false(empty);
    pop(&stack);
    empty = is_stack_empty(stack);
    assert_true(empty);
}

static void test_push_and_pop() {
    Stack *stack = init_stack();
    push(&stack, "A");
    push(&stack, "B");
    push(&stack, "C");
    char *data = pop(&stack);
    assert_string_equal(data, "C");
    push(&stack, "D");
    push(&stack, "E");
    data = pop(&stack);
    assert_string_equal(data, "E");
    data = pop(&stack);
    assert_string_equal(data, "D");
    data = pop(&stack);
    assert_string_equal(data, "B");
    push(&stack, "F");
    data = pop(&stack);
    assert_string_equal(data, "F");
    data = pop(&stack);
    assert_string_equal(data, "A");
    int empty = is_stack_empty(stack);
    assert_true(empty);
}


int main(int argc, char **argv) {
    const struct CMUnitTest tests[] = {
            cmocka_unit_test(test_alloc_stack_node),
            cmocka_unit_test(test_init_stack),
            cmocka_unit_test(test_is_stack_empty),
            cmocka_unit_test(test_push_and_pop),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}
